class Solution {
    public boolean isMatch(String s, String p) {
        int n = s.length() ;
        int m = p.length() ;
        s = " "+s ;
        p = " " +p ;
        char[] sArr = s.toCharArray();
        char[] pArr = p.toCharArray() ;
        boolean [][] dp = new boolean[n+1][m+1] ;
        dp[0][0] = true ;
        for(int j=2 ; j <=m ;j +=2){
            if(pArr[j] == '*'){
                dp[0][j] = true ;
            }else{
                break ;
            }

        }
        for(int i=1 ; i<=n ; i ++){
            for(int j=1 ; j <= m ; j ++){
                if(pArr[j] != '*'){
                    dp[i][j] =(sArr[i]==pArr[j]||pArr[j]=='.') && dp[i-1][j-1] ;
                }else{
                    dp[i][j] = dp[i][j-2] || ((pArr[j-1]=='.' || sArr[i] == pArr[j-1]) && dp[i-1][j]);
                }
            }
        }
        return dp[n][m] ;
    }
}